<?php
namespace Api\Controller;
use Think\Controller;
class UserController extends Controller {
    /**
	 * 24小时api
	 * @param string  
	 * @return   
	 */
	public function index(){
        // $this->display('login');
        $this->show('24小时快递-国内首家先行赔付快递服务商(api/user)');
        $this->display('');
    }
	/**
	 *验证用户登录 	测试地址 http://www.boss24delivery.com/index.php/api/user/checkLogin
	 * @param string 用户名post.phone
	 * @param string 密码post.pwd
	 * @param string 记住密码post.rememberMe	
	 * @return  json
	 */ 
	public function checkLogin(){
		if(IS_POST){
				$pwd	= I('post.pwd');
				$phone	= I('post.phone');
				//$sloat		= generateRandomString(5);
				//$sloat		= '连接数据库获取对应sloat值';
				$member					= D('member');	
				//验证用户名和密码
				$map					= array();	
				$map['status']			= 1;	
				$map['is_del']			= 0;
				$map['phone']			= $phone;
				$map['pwd']				= $pwd;
				//验证用户名
				$map_					= array();	
				$map_['status']			= 1;	
				$map_['is_del']			= 0;
				$map_['phone']			= $phone;
				//$map['pwd']				= $pwd;
				//$sloat		= 'test';
				//$pwd		= 'admin';
				//$token		= md5($sloat.md5($pwd));
				//和数据库校验
				//用户名验证结果
				$res_user				= $member->where($map_)->find();
				//验证密码
				$res_					= $member->where($map)->find();
				$sloat					= $res_['sloat'];
				//用户名错误
				if(!$res_user){
									$data 		= array(
									'status'		=> 202,
									'info' 			=> '用户名错误！',
									//'sloat'			=>  $sloat,
									//'callback' 		=> U('Index/dispalyIndex')
									);
				}else {
						//用户名正确 密码错误
						if(!$res_){
									$data 		= array(
									'status'		=> 201,
									'info' 			=> '密码错误！',
									//'sloat'			=>  $sloat,
									//'callback' 		=> U('Index/dispalyIndex')
									);
						//通过验证		
						}else{
									$data 		= array(
									'status'		=> 200,
<<<<<<< HEAD
<<<<<<< master
									'info' 			=> 'ok',
									'token'			=>  $token,
									'callback' 		=> U('Index/dispalyIndex')
								);
=======
									'info' 			=> '登录成功！',
									'sloat'			=>  $sloat,
									'personalDate'			=>  $res_,
=======
									'info' 			=> '登录成功！',
									'sloat'			=>  $sloat,
>>>>>>> shan
									//'callback' 		=> U('Index/dispalyIndex')
									);
						}
				}
				
<<<<<<< HEAD
>>>>>>> local
=======
>>>>>>> shan
				$this->ajaxReturn($data);
		}else{
				$data 		= array(
									'status'		=> 400,
									'info' 			=> 'bad request!',
									'test'			=> '',
									'callback' 		=> ''
								);
				$this->ajaxReturn($data);
		}
		
    }
	/**
	 *用户注册 	测试地址 http://www.boss24delivery.com/index.php/api/user/userRegister
	 * @param string 用户名post.name
	 * @param string 密码post.pwd
	 * @param string 记住密码post.rememberMe	
	 * @return  json  132--0123
	 */ 
	public function userRegister(){
		if(IS_POST){
				$pwd					= I('post.pwd');
				$phone					= I('post.phone');
				$member					= D('member');	
				//
				$map					= array();	
				$map['status']			= 1;	
				$map['is_del']			= 0;
				$map['phone']			= $phone;
				$userExists				= $member->where($map)->find();
				if(empty($pwd)||empty($phone)){
					$data 		= array(
									'status'		=> 405,
									'info' 			=> '用户名或者密码为空', 
									); 
					$this->ajaxReturn($data);	exit(); 				
					}
				if($userExists){
					$data 		= array(
									'status'		=> 403,
									'info' 			=> '该手机已被注册！请直接登录,如忘记密码可直接用验证码登录！', 
									); 
					$this->ajaxReturn($data);	exit(); 				
					}
				// 检测改手机号是否可以
				$map['name']			= $phone;
				$map['pwd']				= $pwd;
				$map['sloat']			= $this->generateRandomString(6); 
				$res					= $member->data($map)->add();
				//用户名注册成功
				if($res){
									$data 		= array(
									'status'		=> 200,
									'info' 			=> '注册成功', 
									);
				}else {
						// 用户注册失败
						 
									$data 		= array(
									'status'		=> 401,
									'info' 			=> '用户注册失败！', 
									); 			 
						}
				$this->ajaxReturn($data);		
				 
		}else{
				$data 		= array(
									'status'		=> 400,
									'info' 			=> 'bad request!',
									'test'			=> '',
									'callback' 		=> ''
								);
				$this->ajaxReturn($data);
		}
		
    }
	/**
	 *用户名 	测试地址 http://www.boss24delivery.com/index.php/api/user/checkUserExists
	 * @param string 用户名post.phone  手机号码
	 * @param string 密码post.pwd
	 * @param string 记住密码post.rememberMe	
	 * @return  json  132--0123
	 */ 
	public function checkUserExists(){
		if(IS_POST){
				$phone					= I('post.phone');
				$member					= D('member');	
				//验证用户名 
				$map					= array();	
				$map['status']			= 1;	
				$map['is_del']			= 0;
				$map['phone']			= $phone;  
				$res_user				= $member->where($map)->find();
				//手机号码已经存在
				if($res_user){
									$data 		= array(
									'status'		=> 201,
									'info' 			=> '该手机已经被注册,请直接登录！如忘记密码可直接用验证码登录',
									 );
				}else {
						//手机号未被注册
									$data 		= array(
									'status'		=> 200,
									'info' 			=> '手机号可以注册！',
									 );
						}  
				$this->ajaxReturn($data);
		}else{
				$data 		= array(
									'status'		=> 400,
									'info' 			=> 'bad request!',
									'test'			=> '',
									'callback' 		=> ''
								);
				$this->ajaxReturn($data);
		}
		
    }
	public function generateRandomString($length=6){
		$characters='0123456789';
		$randomString='';
		for($i=0;$i<$length;$i++){
			$randomString.=$characters[rand(0,strlen($characters)-1)];
			}
			return$randomString;
	}			
	
	/**
	 *发送验证码 	测试地址 http://www.boss24delivery.com/index.php/api/user/smsCode
	 * @param string 用户名post.phone  手机号码
	 * @param string 密码post.pwd
	 * @param string 记住密码post.rememberMe	
	 * @return  json  132--0123
	 */ 
	public function smsCode(){
		if(IS_POST){
				$phone	= I('post.phone'); 
				$code	= $this->generateRandomString(6);
				//手机号码已经存在  
				$data 		= array(
									'status'		=> 200,
									'code'		=> $code,
									'info' 			=> '已发送！',
									 ); 
				$this->ajaxReturn($data);
		}else{
				$data 		= array(
									'status'		=> 400,
									'info' 			=> 'bad request!',
									'test'			=> '',
									'callback' 		=> ''
								);
				$this->ajaxReturn($data);
		}
		
    }
	/**
	 *验证token 	测试地址 http://www.boss24delivery.com/index.php/api/user/checkUserToken
	 * @param string 用户名post.phone
	 * @param string token值post.token 	
	 * @return  json
	 */ 
	public function checkUserToken($phone,$token){ 
				//$name,$token
				//$phone					= 13269706193;
				//$token					= '';
				$member					= D('member');	 
				$map					= array();	
				$map['status']			= 1;	
				$map['is_del']			= 0;
				$map['phone']			= $phone;
				$res					= $member->where($map)->find();  
				//获取密码和sloat值
				$pwd					= $res['pwd'];
				$sloat					= $res['sloat']; 
				// 拼接token
				$tokenV					= md5('@'.$phone.'#'.md5($pwd.'$'.$sloat.date('y-m-d')));
				if($token==$tokenV){
					return true;
				}else{
					return false;
				} 
				/*$data 		= array(
									'status'		=> 200,
									'info' 			=> 'token验证通过！',
									'sloat'			=>  $sloat,
									'token'			=>  $token,
									'date'			=>  date('y-m-d'),
									//'callback' 		=> U('Index/dispalyIndex')
									); 
				$this->ajaxReturn($data);*/  
    } 
	 
	 

}